package com.hitalkie.teacher.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hitalkie.teacher.R;
import com.hitalkie.teacher.app.TalkieApi;
import com.hitalkie.teacher.fragment.LoadingDialogFragment;
import com.hitalkie.teacher.model.BaseResponse;
import com.hitalkie.teacher.util.OkHttpManager;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

import static com.hitalkie.teacher.app.TalkieApi.getHost;

/**
 * Date 06/01/2017
 *
 * @author charliema
 */

public class InfoActivity extends BaseActivity {

    private String mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        updateData();
    }

    private void initView() {
        setContentView(R.layout.view_info);
        initTitleBar();
    }

    private void initTitleBar() {
        findViewById(R.id.viewTitleBarLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    private void initData() {
        mType = getIntent().getStringExtra("type");
    }

    private void updateData() {
        // loading dialog
        final LoadingDialogFragment loadingDialogFragment = new LoadingDialogFragment();
        loadingDialogFragment.show(getSupportFragmentManager(), "loading");

        String url = getHost() + TalkieApi.PATH_INFO + "?type=" + mType;
        Request.Builder reqBuilder = new Request.Builder().url(url);
        OkHttpManager.client.newCall(reqBuilder.build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // loading dialog
                                loadingDialogFragment.dismiss();
                                Toast.makeText(getActivity(), getText(R.string.network_exception), Toast.LENGTH_LONG).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        // loading dialog
                        loadingDialogFragment.dismiss();
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();

                        try {
                            final InfoResp resp = new Gson().fromJson(jsonStr, InfoResp.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {
                                        updateContentView(resp.info);
                                    } else {
                                        Toast.makeText(getActivity(), resp.status.error_message, Toast.LENGTH_LONG).show();
                                    }

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            // loading dialog
                            loadingDialogFragment.dismiss();
                        }
                    }
                });
    }

    private void updateContentView(String info) {
        TextView tvContent = (TextView) findViewById(R.id.tvContent);
        tvContent.setText(info);
    }


    private static class InfoResp extends BaseResponse {
        String info;
    }
}
